package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.GroupCountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description: 删除时可以删除一组；减少对 key 的管理消耗；
 * 不像 INCR 有自增1 的命令。
 */
@RestController
@RequestMapping("group/counter")
@Api(tags = "分组计数器：可用于同一组下多个计数器")
@Slf4j
public class GroupCountController {

    final String PV_KEY_NAME = "pv::counter";
    final String DL_KEY_NAME = "dl::counter";

    @Autowired
    GroupCountService groupCountService;

    @GetMapping("/pvIncrement/{path}")
    @ApiOperation("页面浏览量的计数增加")
    public long pvIncrement(@PathVariable("path") String path) {
        long increase = groupCountService.increase(PV_KEY_NAME, path);
        return increase;
    }

    @GetMapping("/dlIncrement/{path}")
    @ApiOperation("页面浏览量的计数增加")
    public long dlIncrement(@PathVariable("path") String path) {
        long increase = groupCountService.increase(DL_KEY_NAME, path);
        return increase;
    }
}
